package com.xywy.me.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.xywy.common.greendao.DaoSession;
import com.xywy.me.bean.User;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

public class UserDao extends AbstractDao<User, Long> {

    public static final String TABLENAME = "xywy_user";

    /**
     * Properties of entity User.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Userid = new Property(1, String.class, "userid", false, "USERID");
        public final static Property Isregister = new Property(2, String.class, "isregister", false, "ISREGISTER");
        public final static Property Phonenum = new Property(3, String.class, "phonenum", false, "PHONENUM");
        public final static Property Height = new Property(4, String.class, "height", false, "HEIGHT");
        public final static Property Sex = new Property(5, String.class, "sex", false, "SEX");
        public final static Property Birthday = new Property(6, String.class, "birthday", false, "BIRTHDAY");
        public final static Property Avatar = new Property(7, String.class, "avatar", false, "AVATAR");
        public final static Property Email = new Property(8, String.class, "email", false, "EMAIL");
        public final static Property Last_login_time = new Property(9, Long.class, "last_login_time", false, "LAST_LOGIN_TIME");
        public final static Property Is_current = new Property(10, Boolean.class, "is_current", false, "IS_CURRENT");
        public final static Property Accountstr = new Property(11, String.class, "accountstr", false, "ACCOUNTSTR");
    };


    public UserDao(DaoConfig config) {
        super(config);
    }
    
    public UserDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'xywy_user' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'USERID' TEXT," + // 1: userid
                "'ISREGISTER' TEXT," + // 2: isregister
                "'PHONENUM' TEXT," + // 3: phonenum
                "'HEIGHT' TEXT," + // 4: height
                "'SEX' TEXT," + // 5: sex
                "'BIRTHDAY' TEXT," + // 6: birthday
                "'AVATAR' TEXT," + // 7: avatar
                "'EMAIL' TEXT," + // 8: email
                "'LAST_LOGIN_TIME' INTEGER," + // 9: last_login_time
                "'IS_CURRENT' INTEGER," + // 10: is_current
                "'ACCOUNTSTR' TEXT);"); // 11: accountstr
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'xywy_user'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, User entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String userid = entity.getUserid();
        if (userid != null) {
            stmt.bindString(2, userid);
        }
 
        String isregister = entity.getIsregister();
        if (isregister != null) {
            stmt.bindString(3, isregister);
        }
 
        String phonenum = entity.getPhonenum();
        if (phonenum != null) {
            stmt.bindString(4, phonenum);
        }
 
        String height = entity.getHeight();
        if (height != null) {
            stmt.bindString(5, height);
        }
 
        String sex = entity.getSex();
        if (sex != null) {
            stmt.bindString(6, sex);
        }
 
        String birthday = entity.getBirthday();
        if (birthday != null) {
            stmt.bindString(7, birthday);
        }
 
        String avatar = entity.getAvatar();
        if (avatar != null) {
            stmt.bindString(8, avatar);
        }
 
        String email = entity.getEmail();
        if (email != null) {
            stmt.bindString(9, email);
        }
 
        Long last_login_time = entity.getLast_login_time();
        if (last_login_time != null) {
            stmt.bindLong(10, last_login_time);
        }
 
        Boolean is_current = entity.getIs_current();
        if (is_current != null) {
            stmt.bindLong(11, is_current ? 1l: 0l);
        }
 
        String accountstr = entity.getAccountstr();
        if (accountstr != null) {
            stmt.bindString(12, accountstr);
        }
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public User readEntity(Cursor cursor, int offset) {
        User entity = new User( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // userid
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // isregister
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // phonenum
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // height
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // sex
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // birthday
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // avatar
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // email
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9), // last_login_time
            cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0, // is_current
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11) // accountstr
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, User entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setUserid(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setIsregister(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setPhonenum(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setHeight(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setSex(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setBirthday(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setAvatar(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setEmail(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setLast_login_time(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
        entity.setIs_current(cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0);
        entity.setAccountstr(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(User entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(User entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
}